package com.longmao.educenter.controller;

import com.longmao.commonutils.JwtUtils;
import com.longmao.commonutils.R;
import com.longmao.commonutils.ordervo.UcenterMemberOrder;
import com.longmao.educenter.entity.UcenterMember;
import com.longmao.educenter.entity.vo.RegisterVo;
import com.longmao.educenter.service.UcenterMemberService;
import com.longmao.servicebase.handler.LongMaoException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author longmao
 * @since 2022-02-21
 */
@RestController
//@CrossOrigin
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录
     * @param userInfo
     * @return
     */
    @PostMapping("/login")
    public R loginUser(@RequestBody UcenterMember userInfo) {
        String token = memberService.login(userInfo);
        return R.ok().data("token", token);
    }

    /**
     * 注册
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        boolean result = memberService.register(registerVo);
        if(result) {
            return R.ok();
        }else {
            return R.error().message("注册失败");
        }
    }

    /**
     * 根据token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        try {
            //获取用户头信息
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            //根据用户id查询用户信息
            UcenterMember ucenterMember = memberService.getById(memberId);
            return R.ok().data("userInfo", ucenterMember);
        }catch (Exception e){
            e.printStackTrace();
            throw new LongMaoException(20001,"获取失败");
        }
    }

    /**
     * 根据用户id查询用户信息
     * @param memberId
     * @return
     */
    @PostMapping("/getMemberInfoById/{memberId}")
    public UcenterMemberOrder getMemberInfoById(@PathVariable String memberId){
        UcenterMember member = memberService.getById(memberId);
        UcenterMemberOrder memberVo = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,memberVo);

        return memberVo;
    }

    /**
     * 根据用户id获取用户信息
     * @param id
     * @return
     */
    @PostMapping("/getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    /**
     * 统计某一天注册人数
     * @param day
     * @return
     */
    @GetMapping("/countregister/{day}")
    public R registerCount(@PathVariable String day){
        Integer count = memberService.countRegisterByDay(day);
        return R.ok().data("countRegister", count);
    }
}

